﻿using AutoMapper;
using Google.Protobuf.WellKnownTypes;
using ManagementApi.Models;
using ManagementApi.Models.Cms;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Cms;
using Modules.Cms.Entitys;
using Modules.System.User;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Controllers.Cms;

/// <summary>
/// 出库单相关业务接口
/// </summary>
[Route("/api/v1/cmsOrder")]
[ApiController]
[Authorize()]
public class CmsOrderController : ControllerBase
{
    private readonly CmsOrderService cmsOrderService;
    private readonly IApplicationContext applicationContext;
    private readonly IMapper mapper;

    public CmsOrderController(CmsOrderService cmsOrderService, IApplicationContext applicationContext, IMapper mapper)
    {
        this.cmsOrderService = cmsOrderService;
        this.mapper = mapper;
        this.applicationContext = applicationContext;
    }


    /// <summary>
    /// 获取出库单列表
    /// </summary>
    /// <param name="query">查询条件</param>
    /// <param name="pageNum">页码</param>
    /// <param name="pageSize">每页数量</param>
    /// <returns></returns>
    [HttpGet("page")]
    public ApiResult<PageDto<CmsOrderDto>> GetRoles([FromQuery] CmsOrderQuery query, int pageNum = 1, int pageSize = 20)
    {
        var roles = cmsOrderService.GetPages(query, pageNum, pageSize);
        var dtos = roles.AsPageDto(source => { return source.AsDto(); });
        return ApiResult<PageDto<CmsOrderDto>>.Success(dtos);
    }

    /// <summary>
    /// 创建出库单
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    [HttpPost("create")]
    public ApiResult Add(CmsOrderEditForm form)
    {
        CmsOrder entity = new CmsOrder();
        mapper.Map(form, entity);
        entity.CreateDt = DateTime.Now;
        entity.CreateUserId = applicationContext.CurrentUser?.Id ?? 0;
        entity.CreateUserName = applicationContext.CurrentUser?.Name ?? "";
        entity.Type = 10;
        entity.ConsigneeStatus = 10;
        entity.BillNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        cmsOrderService.Create(entity);
        return ApiResult.Success("创建成功");
    }

    /// <summary>
    /// 修改出库单
    /// </summary>
    /// <param name="id"></param>
    /// <param name="form"></param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public ApiResult Edit(int id, CmsOrderEditForm form)
    {
        CmsOrder? entity = cmsOrderService.Get(id);
        if (entity == null) return ApiResult.Failure("数据不存在");
        mapper.Map(form, entity);
        entity.UpdateDt = DateTime.Now;
        entity.UpdateUserId = applicationContext.CurrentUser?.Id ?? 0;
        entity.UpdateUserName = applicationContext.CurrentUser?.Name ?? "";
        cmsOrderService.Update(entity);
        return ApiResult.Success("编辑成功");
    }

    /// <summary>
    /// 收货
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    [HttpPost("returnOrder")]
    public ApiResult ReturnOrder(CmsReturnOrderForm form)
    {
        CmsOrder? entity = cmsOrderService.Get(form.Id);
        if (entity == null) return ApiResult.Failure("数据不存在");
        entity.ConsigneeStatus = 20;
        entity.ConsigneeDt = form.ConsigneeDt;
        entity.Consignee= form.Consignee;
        cmsOrderService.Update(entity);
        return ApiResult.Success("操作成功");
    }

    /// <summary>
    /// 删除出库单
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public ApiResult Delete(int id)
    {
        cmsOrderService.Delete(id);
        return ApiResult.Success("删除成功");
    }

    /// <summary>
    /// 出库单详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public ApiResult<CmsOrderEditForm> Get(int id)
    {
        CmsOrder? entity = cmsOrderService.Get(id);
        if (entity == null) return ApiResult<CmsOrderEditForm>.Failure("数据不存在");
        CmsOrderEditForm data = mapper.Map<CmsOrderEditForm>(entity);
        data.DealerName = ServiceAccessor.GetService<CmsDealerService>().Get(data.DealerId)?.Name ?? "";
        return ApiResult<CmsOrderEditForm>.Success(data);
    }
}
